//
//  AUProtocol_Native.swift
//  AwesomeUBaseModule_Example
//
//  Created by Miss_Special on 2018/4/13.
//  Copyright © 2018 CocoaPods. All rights reserved.
//

import AVFoundation

public protocol AUProtocol_Native {
    
    /// Camera: Whether camera is enable
    ///
    /// - Parameters:
    ///   - showAlert: whether show alert controller when denied, default is false
    ///   - title: Alert title
    ///   - message: Alert message
    ///   - okActionTitle: Ok action title
    ///   - cancelActionTitle: Cancel action title
    /// - Returns: Whether camera is enable
    func au_isCameraEnable(
        showAlertWhenDenied showAlert: Bool,
        unableAlertTitle title: String,
        unableAlertMessage message: String,
        alertOkActionTitle okActionTitle: String,
        alertCancelActionTitle cancelActionTitle: String) -> Bool
}

// MARK: - Camera
public extension AUProtocol_Native {
    
    /// Get status
    func au_isCameraEnable(showAlertWhenDenied showAlert: Bool = false,
                           unableAlertTitle title: String = "相机不可用",
                           unableAlertMessage message: String = "去设置?",
                           alertOkActionTitle okActionTitle: String = "设置",
                           alertCancelActionTitle cancelActionTitle: String = "取消") -> Bool {
        // 初始状态
        var authOK = false
        
        // 是否决定过
        var notDetermined = false
        // 新的授权状态
        var accessOK = false
        let semaphore = DispatchSemaphore(value: 0)
        
        switch AVCaptureDevice.authorizationStatus(for: .video) {
        case .notDetermined:
            notDetermined = true
            AVCaptureDevice.requestAccess(for: .video) { (success) in
                print(success)
                accessOK = success
                semaphore.signal()
            }
        case .restricted, .denied:
            if showAlert {
                showCameraUnableAlert(unableAlertTitle: title, unableAlertMessage: message, alertOkActionTitle: okActionTitle, alertCancelActionTitle: cancelActionTitle)
            }
        case .authorized:
            authOK = true
        }
        
        if notDetermined {
            semaphore.wait()
            return accessOK
        } else {
            return authOK
        }
    }
    
    /// Alert
    func showCameraUnableAlert(unableAlertTitle title: String,
                               unableAlertMessage message: String,
                               alertOkActionTitle okActionTitle: String,
                               alertCancelActionTitle cancelActionTitle: String) {
        // 没有权限
        let alertVC = UIAlertController(title: title, message: message, preferredStyle: .alert)
        let okAction = UIAlertAction(title: okActionTitle, style: .destructive, handler: { (_) in
            let url = URL(string: UIApplicationOpenSettingsURLString)
            if UIApplication.shared.canOpenURL(url!) {
                UIApplication.shared.openURL(url!)
            }
        })
        let cancelAction = UIAlertAction(title: cancelActionTitle, style: .cancel, handler: nil)
        alertVC.addAction(okAction)
        alertVC.addAction(cancelAction)
        UIApplication.shared.keyWindow?.rootViewController?.present(alertVC, animated: true, completion: nil)
    }
}
